package com.ruoyi.finance.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.finance.domain.AccountSet;
import com.ruoyi.finance.domain.DefaultAccountSet;
import com.ruoyi.finance.mapper.DefaultAccountSetMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.finance.mapper.AccountSetMapper;
import com.ruoyi.finance.service.IAccountSetService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 账套管理Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-24
 */
@Service
public class AccountSetServiceImpl extends ServiceImpl<AccountSetMapper, AccountSet> implements IAccountSetService
{
    @Autowired
    private AccountSetMapper accountSetMapper;

    /**
     * 查询账套管理
     *
     * @param accountId 账套管理主键
     * @return 账套管理
     */
    @Override
    public AccountSet selectAccountSetByAccountId(Long accountId)
    {
        return accountSetMapper.selectAccountSetByAccountId(accountId);
    }

    /**
     * 查询账套管理列表
     *
     * @param accountSet 账套管理
     * @return 账套管理
     */
    @Override
    public List<AccountSet> selectAccountSetList(AccountSet accountSet)
    {
        return accountSetMapper.selectAccountSetList(accountSet);
    }

    /**
     * 新增账套管理
     *
     * @param accountSet 账套管理
     * @return 结果
     */
    @Override
    public int insertAccountSet(AccountSet accountSet)
    {

        List<AccountSet> list = lambdaQuery().eq(AccountSet::getAccountName, accountSet.getAccountName()).list();
        if(list.size()>0&& list!=null){
            throw new RuntimeException("账套名称已存在");
        }
        return accountSetMapper.insert(accountSet);
    }

    /**
     * 修改账套管理
     *
     * @param accountSet 账套管理
     * @return 结果
     */
    @Override
    public int updateAccountSet(AccountSet accountSet)
    {
        return accountSetMapper.updateAccountSet(accountSet);
    }

    /**
     * 批量删除账套管理
     *
     * @param accountIds 需要删除的账套管理主键
     * @return 结果
     */
    @Override
    public int deleteAccountSetByAccountIds(Long[] accountIds)
    {
        return accountSetMapper.deleteAccountSetByAccountIds(accountIds);
    }

    /**
     * 删除账套管理信息
     *
     * @param accountId 账套管理主键
     * @return 结果
     */
    @Override
    public int deleteAccountSetByAccountId(Long accountId)
    {
        return accountSetMapper.deleteAccountSetByAccountId(accountId);
    }

    @Override
    public List<AccountSet> listAccountSetAllByUserId() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        return accountSetMapper.listAccountSetAllByUserId(loginUser.getUserId());
    }


    @Autowired
    private DefaultAccountSetMapper defaultAccountSetMapper;
    @Override
    @Transactional
    public int checkDefaultAccountSet(Long accountId) {
        //获取当前用户
        LoginUser loginUser = SecurityUtils.getLoginUser();

        LambdaQueryWrapper<DefaultAccountSet> defaultAccountSetLambdaQueryWrapper = new LambdaQueryWrapper<>();
        defaultAccountSetLambdaQueryWrapper.eq(DefaultAccountSet::getUserId,loginUser.getUserId());
        List<DefaultAccountSet> defaultAccountSets = defaultAccountSetMapper.selectList(defaultAccountSetLambdaQueryWrapper);


        if(defaultAccountSets.size()>0){
            DefaultAccountSet defaultAccountSet = defaultAccountSets.get(0);
            if(defaultAccountSet.getAccountSetId().equals(accountId)){
                throw new RuntimeException("当前用户已设置默认账套");
            }
            defaultAccountSetMapper.delete(defaultAccountSetLambdaQueryWrapper);
        }
        DefaultAccountSet defaultAccountSet = new DefaultAccountSet();
        defaultAccountSet.setUserId(loginUser.getUserId());
        defaultAccountSet.setAccountSetId(accountId);
        return defaultAccountSetMapper.insert(defaultAccountSet);

    }
}
